package com.example.demo2.test.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.demo2.test.model.MarketEntity;
import com.example.demo2.test.websocket.BinanceWebSocket;
import com.example.demo2.test.websocket.HuobiWebSocket;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;

/**
 * Description:
 *
 * @author my.miao
 * @date 2022/1/5 15:03
 */
@Slf4j
@Component
public  class HuobiService {
//    @Resource
//    private SwapMarketMapper marketMapper;

//    @Resource
//    private TradeService tradeService;
    @Resource
    private ExecutorService executorService;

    private static String DEPTH = "depth";
    private static String DETAIL = "detail";
    private static String MARK_PRICE = "mark-price";
    private static Map<String,String> symbolMap = new HashMap<>();// 对应请求数据的交易对
    private static Map<String,String> subTypeMap = new HashMap<>();// 对应请求数据的类型

    @Value("${huobi.socket.api}")
    private String api;

    private HuobiWebSocket webSocket;


    public void message(String text) {
        log.info("收到消息了" + text);
    }

    public void onMarket() {
        webSocket = HuobiWebSocket.createConnectionManage(this, api);
        List<MarketEntity> list = new ArrayList<>();// 查询要采集的交易对
        list.add(new MarketEntity("vet_usdt", "binance01",0,1 ));
        list.add(new MarketEntity("btc_usdt", "binance02",0,1 ));

        List<String> subDepthList = new ArrayList<>();
        List<String> subTradeList = new ArrayList<>();
        for(MarketEntity item : list) {
            String symbol = item.getSymbol().replace("_","");// 调整交易对
            subDepth(symbol,item.getSymbol());// 订阅盘口
            subDepthList.add(item.getSymbol());
            if(item.getTradeLog().equals(1)){// 判断是否订阅成交记录
                subTrade(symbol,item.getSymbol());// 订阅成交记录
                subTradeList.add(item.getSymbol());
            }
        }

    }

    public void subTrade(String symbol,String symbolOld){

        StringBuilder subTrade = new StringBuilder();
        subTrade.append("market.").append(symbol).append(".trade.detail");
        symbolMap.put(subTrade.toString(),symbolOld);
        subMessage(subTrade.toString());//交易明细
    }

    public void subDepth(String symbol,String symbolOld){
        StringBuilder subDepth = new StringBuilder();
        subDepth.append("market.").append(symbol).append(".mbp.refresh.5");
        symbolMap.put(subDepth.toString(),symbolOld);
        subMessage(subDepth.toString());//买一卖一盘 最新价
    }

    /**
     * 订阅websocket的数据
     * @param subStr
     */
    private void subMessage(String subStr) {
        // 参数校验(简单的为空判断)
        if (webSocket == null) {
            throw new RuntimeException("HuobiWebSocket 不能为空");
        }
        if(StringUtils.isEmpty(subStr)){
            throw new RuntimeException("传递的订阅信息非法");
        }
        // 组拼发送的消息
        JSONObject json = new JSONObject();
        json.put("id", System.nanoTime());// 设置id
        json.put("sub", subStr);
        webSocket.send(json.toJSONString());
    }

    public void Kxian() {
        webSocket = HuobiWebSocket.createConnectionManage(this, api);
        JSONObject js = new JSONObject();
        js.put("sub","market.ethbtc.kline.1min");
        js.put("id","id1");
        webSocket.send(js.toJSONString());
    }

    public void depth() {
        webSocket = HuobiWebSocket.createConnectionManage(this, api);
        JSONObject js = new JSONObject();
        js.put("sub","market.btcusdt.depth.step0");
        js.put("id","id1");
        webSocket.send(js.toJSONString());
    }

}
